package GoldMiner.com.ALi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.ArrayList;

/**
 * @author ：ALi
 * @date ：Created in 2021/10/31 9:11
 * @description：窗口绘制
 * @modified By：
 * @version: $
 */
public class GameWin extends JFrame {

    //加载背景图类
    BackGroundImgs bg = new BackGroundImgs();
    //划线类
    Line l = new Line(this);
    //金块
//    Gold gold = new Gold();//不在需要，直接在金块集合中用
    //画布
    Image offScreenImage;

    //金块集合.存储金块，石块
    List<Object> objectList = new ArrayList<>();
    //游戏状态 0:准备开始，1：运行中，2：商店，3：失败，4：胜利
    protected static int state;

    //代码块设置个数
    {
        //是否可以放置
        boolean isPlace = true;
        //金块
        for (int i = 0;i <= 10;i++){
            double random = Math.random();//按照比例生成大小中金块
            Gold gold;//存放到当前金块，来判断位置
            if(random<0.3){
                gold = new GoldMini();

            }else if(random<0.7){
                gold = new Gold();

            }else{
                gold = new GoldPlus();

            }
            for (Object obj:objectList){
                if (gold.getRec().intersects(obj.getRec())){
                    //重叠了不放置,重新生成
                    isPlace = false;
                }
            }
            if(isPlace == true){
                objectList.add(gold);
            }else{
                isPlace = true;
                i--;
            }

        }
        //石块
        for (int i = 0;i <= 2;i++){
            Rock rock = new Rock();
            //判断是否重合
            for(Object obj:objectList){
                if(rock.getRec().intersects(obj.getRec())){
                    isPlace = false;
                }
            }
            if(isPlace == true){
                objectList.add(rock);
            }else {
                isPlace = true;
                i--;
            }

        }
    }



    /**
     * 加载窗口函数
     */
    void launch(){
        this.setVisible(true);//窗口可见
        this.setSize(768,1000);//窗口大小
        this.setLocationRelativeTo(null);//窗口居中
        this.setTitle("黄金矿工");

        setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭后关闭窗口

        //鼠标事件
        /**
         * create by: ALi
         * description: 鼠标点击事件
         * create time:  10:13
         * @Param: 1 鼠标左键，2 滚轮，3 鼠标右键
         * @return void
         */
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        //右键开始游戏
                        if(e.getButton() == 3){
                            state = 1;
                            BackGroundImgs.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        //左右摇摆，点击左键抓取
                        if(e.getButton() == 1){
                            l.state = 1;
                        }
                        //抓取返回
                        if(e.getButton() == 3 && l.state == 3 && BackGroundImgs.WaterNamber > 0){
                            BackGroundImgs.WaterNamber--;
                            BackGroundImgs.WaterFlag = true;
                        }
                        break;
                    case 2:
                        if(e.getButton() == 1){
                            bg.shop = true;
                        }else if(e.getButton() == 3){
                            state = 1;
                            BackGroundImgs.startTime = System.currentTimeMillis();

                        }
                        break;
                    case 3:
                    case 4:
                        if(e.getButton() == 1){
                            state = 0;
                            bg.reGame();
                            l.reGame();
                        }
                        break;
                    default:
                }

            }
        });

        //一直绘图片
        while(true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * create by: ALi
     * description: 下一关
     * create time: 2021/10/31 14:18
     *
     * @Param: null
     * @return
     */
    public void nextLevel(){
        if(state == 1 && bg.gameTime() == true){

            if (BackGroundImgs.count >= BackGroundImgs.goal){

                if(BackGroundImgs.level == 5){
                    state = 4;//成功
                }else{
                    state = 2;
                    BackGroundImgs.startTime = System.currentTimeMillis();
                    BackGroundImgs.level++;
                    //目标数设置为leve*15
                    BackGroundImgs.goal = BackGroundImgs.level*15;
//                    //当前得分为0；
//                    BackGroundImgs.count = 0;
//                    state = 1;

                }


            }else{
                    state = 3;
            }
            //释放之前的窗体
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();

        }

    }


    /**
     * 重写paint方法，绘制图片。将背景图片与窗口进行关联，并且将起始坐标为（0,0）
     * @param g 画图的参数
     */
    @Override
    public void paint(Graphics g) {
        //定义画布可以解决闪动问题，类似于一层一层画布覆盖了，之前是覆盖背景再画物体，
        //现在是将所有物体画在画布上，然后将画布画在背景上
        offScreenImage = this.createImage(768,1000);
        //给画布添加画笔
        Graphics gImage = offScreenImage.getGraphics();

        //绘制背景图片
        bg.painImgs(gImage);

        //线和物体在游戏中绘制
        if(state ==1 ){
            //从金块集合中画金块(多个金块)
            for (Object obj:objectList){
                obj.PaintLump(gImage);
            }
            //绘制红线
            l.paintLine(gImage);
        }

        //绘制画布
        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String [] str){
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }

}